#ifndef UTILS_H
#define UTILS_H

#ifdef _WIN32
#include <Windows.h>
#endif

#ifndef NO_GL_GLEXT_PROTOTYPES
#define GL_GLEXT_PROTOTYPES
#endif

#ifdef GLES2
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#else
#ifdef _WIN32
#include <GL/glew.h>
#endif
#include <GL/gl.h>
#endif

typedef unsigned int uint;
#ifdef _WIN32
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
#endif

double mtime();

inline float refitf(float val, float cmin, float cmax, float nmin, float nmax)
{
  return (val-cmin)/(cmax-cmin)*(nmax-nmin)+nmin;
}

GLuint compileShader(GLenum shaderType, const char *src, int srcSize);
void printInfoLog(GLuint handle);

//#define ASSERT_GLERROR() assert(glGetError() == 0)
#define ASSERT_GLERROR()

class Random
{
public:
  static const unsigned int MAX = 0x7fffffff;

  Random(unsigned int seed=0) { m_seed = seed; }

  void seed(unsigned int seed) { m_seed = seed; }

  uint next() { m_seed = m_seed * 1103515245 + 12345; return m_seed&MAX; }
  uint next(uint low, uint high) { return (next()%(high+1-low))+low; }
  float nextfloat() { static const float invmax=1.0f/MAX; return float(next())*invmax; }
  float nextfloat(float low, float high) { return nextfloat()*(high-low)+low; }

private:
  uint m_seed;
};

extern Random g_rand;

#endif
